<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\cache\driver\Redis;


class Search extends Api {
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    /**
     * 添加搜索记录
     * @param string search_word 搜索值
     */
    public function addSearchRecord() {
        $redis =new Redis();
        $uid = $this->auth->id;
        if ($uid){
            $keywords = input('keywords', '');
            $redis_key = 'shanzuoshop_search_record_' . $uid;
            $redis = $redis->handler();

            $search_data = $redis->lrange($redis_key, 0, -1);
            $list_count = $redis->llen($redis_key);
            $c_in = array_search($keywords, $search_data);
            if ($c_in !== false) {
                if ($c_in == 0) {
                    $redis->ltrim($redis_key, 1, -1);
                } else if ($c_in == ($list_count - 1)) {
                    $redis->ltrim($redis_key, 0, -2);
                } else {
                    $redis->lrem($redis_key, $keywords, 0);
                }
            }
            $redis->lpush($redis_key, $keywords);
            if ($redis->llen($redis_key) > 30) {
                $redis->rpop($redis_key);
            }
        }

        $this->success('添加成功!', null);
    }

    /**
     * 获取搜索记录
     * @param int $limit 需要多少条数据
     */
    public function getSearchRecord() {
        $redis =new Redis();
        $uid = $this->auth->id;
        $ret=[];
        if ($uid){
            $limit = input('limit',1);
            $ret = [];
            $redis_key = 'shanzuoshop_search_record_' . $uid;
            $redis = $redis->handler();
            $search_data = $redis->lrange($redis_key, 0, -1);
            $i = 0;
            foreach ($search_data as $keys => $values) {
                $ret[$i]['id'] = $keys;
                $ret[$i]['name'] = $values;
                $i++;
            }
            $ret = array_slice($ret, 0, $limit);
        }

        $this->success('请求成功!', ['list'=>$ret]);

    }

    /**
     * 批量删除搜索历史
     */
    public function delKeywords() {
        $uid = $this->auth->id;
        if ($uid){
            $search_word = $this->request->request('search_word');

            $words = explode(',', $search_word);

            foreach ($words as $key => $item) {
                $this->delKeywordsFun($item, $uid);
            }
        }

        $this->success('删除成功!', null);
    }

    /**
     * 删除搜索历史
     */
    public function delKeywordsFun($keywords, $uid) {
        $redis =new Redis();
        $redis_key = 'shanzuoshop_search_record_' . $uid;
        $redis = $redis->handler();
        return $redis->lrem($redis_key, $keywords, 1);
    }

    /**
     * 全部删除
     */
    public function delAllKeyWords(){
        $uid = $this->auth->id;
        if ($uid) {
            $redis =new Redis();

            $redis_key = 'shanzuoshop_search_record_' . $uid;
            $redis->rm($redis_key);

            // Cache::rm($redis_key);
        }
        $this->success('删除成功!', null);

    }
}